package org.apache.flink.runtime.consumermanager.scheduler;

import org.apache.flink.runtime.ResourceException;

/** Indicates resource allocation failures. */
public class NoResourceAvailableException extends ResourceException {

    private static final long serialVersionUID = -2249953165298717803L;

    private static final String BASE_MESSAGE =
            "Not enough free slots available to resource alloc. "
                    + "You can decrease the operator parallelism or increase the number of slots per consumption in the configuration.";

    public NoResourceAvailableException() {
        super(BASE_MESSAGE);
    }

    public NoResourceAvailableException(String message) {
        super(message);
    }

    public NoResourceAvailableException(String message, Throwable cause) {
        super(message, cause);
    }

    public static NoResourceAvailableException withoutStackTrace(String message) {
        NoResourceAvailableException exception = new NoResourceAvailableException(message);
        exception.setStackTrace(new StackTraceElement[0]);
        return exception;
    }

    // --------------------------------------------------------------------------------------------

    @Override
    public boolean equals(Object obj) {
        return obj instanceof NoResourceAvailableException
                && getMessage().equals(((NoResourceAvailableException) obj).getMessage());
    }

    @Override
    public int hashCode() {
        return getMessage().hashCode();
    }
}
